<?
include_once "date_utils.php";

$filter_hashtag= mysql_real_escape_string($_GET['filter_hashtag']);

$filter_date=$_GET['filter_date'];

//Cálculo de fechas
$todayDB = date("Y-m-d");
$today = date("d-m-Y");

$fecha = date_create($todayDB);
date_add($fecha, date_interval_create_from_date_string('1 days'));
$tomorrowDB = date_format($fecha, 'Y-m-d');
$tomorrow = date_format($fecha, 'd-m-Y');

$fecha = date_create($todayDB);
date_add($fecha, date_interval_create_from_date_string('7 days'));
$weekDB = date_format($fecha, 'Y-m-d');
$week = date_format($fecha, 'd-m-Y');

$fecha = date_create($todayDB);
date_add($fecha, date_interval_create_from_date_string('1 year'));
$yearDB = date_format($fecha, 'Y-m-d');
$year = date_format($fecha, 'd-m-Y');

$fecha = date_create($todayDB);
date_sub($fecha, date_interval_create_from_date_string('1 days'));
$yesterdayDB = date_format($fecha, 'Y-m-d');


$filter_min_date = "";
$filter_min_dateDB = "";
$filter_max_date = "";
$filter_max_dateDB = "";

switch ($filter_date) {
  case 'today':
    $filter_min_dateDB=$todayDB;
    $filter_max_dateDB=$todayDB;
    break;
  case 'tomorrow':
    $filter_min_dateDB=$tomorrowDB;
    $filter_max_dateDB=$tomorrowDB;
    break;
  case 'week':
    $filter_min_dateDB=$tomorrowDB;
    $filter_max_dateDB=$tomorrowDB;
    break;
    
  case 'yesterday':
    $filter_min_dateDB = $yesterdayDB;
    $filter_max_dateDB = $yesterdayDB;
    break;
  case 'lastweek':
    $fecha = date_create($todayDB);
    date_sub($fecha, date_interval_create_from_date_string('7 days'));
    $filter_min_dateDB = date_format($fecha, 'Y-m-d');
    $filter_max_dateDB = $yesterdayDB;
    break;
  case 'lastmonth':
    $fecha = date_create($todayDB);
    date_sub($fecha, date_interval_create_from_date_string('30 days'));
    $filter_min_dateDB = date_format($fecha, 'Y-m-d');
    $filter_max_dateDB = $yesterdayDB;
    break;
  case 'lastyear':
    $fecha = date_create($todayDB);
    date_sub($fecha, date_interval_create_from_date_string('1 year'));
    $filter_min_dateDB = date_format($fecha, 'Y-m-d');
    $filter_max_dateDB = $yesterdayDB;
    break;
  
  case "":
    $filter_min_dateDB=$todayDB;
    $filter_max_dateDB="";
    break;
    
  default:
    $filter_dates = explode(",",$filter_date);

    if(isValidDate($filter_dates[0])){
      $filter_min_dateDB = convertDateToDbFormat($filter_dates[0],"-");
    }else{
      $filter_min_dateDB=$todayDB;
    }
    
    if(isValidDate($filter_dates[1])){
      $filter_max_dateDB = convertDateToDbFormat($filter_dates[1],"-");
    }   
    
    if(!empty($filter_max_dateDB) && ($filter_max_dateDB < $filter_min_dateDB))
    {
      $filter_max_dateDB = $filter_min_dateDB;
    }  
    break;
}


//Preparar fechas para mostrar en dataPickers
$filter_min_date=convertDateToUserFormat($filter_min_dateDB);
$filter_max_date=convertDateToUserFormat($filter_max_dateDB);   

?>